package com.lzy.compent;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * @description:
 * @author: Lzy
 * @create: 2020-11-27 16:08
 **/
@Component
@Slf4j
public class KeyResolverConfig {

    @Bean
    public KeyResolver ipKeyResolver() {
        return exchange -> {
            //获取远程客户端IP
            String hostName = Objects.requireNonNull(exchange
                    .getRequest()
                    .getRemoteAddress())
                    .getAddress()
                    .getHostAddress();
            log.info("请求主机ip-->{}", hostName);
            return Mono.just(hostName);
        };
    }
}
